package com.dulian.wallet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dulian.wallet.entity.UserWallet;
import com.dulian.wallet.entity.UserWalletRecord;
import com.dulian.wallet.mapper.UserWalletMapper;
import com.dulian.wallet.service.UserWalletRecordService;
import com.dulian.wallet.service.UserWalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class UserWalletServiceImpl extends ServiceImpl<UserWalletMapper, UserWallet> implements UserWalletService {


    @Autowired
    private UserWalletRecordService userWalletRecordService;
    /**
     * 查询用户钱包余额
     * @param userId
     * @return
     */
    @Override
    public BigDecimal getUserBalance(String userId) {

        QueryWrapper<UserWallet> userWalletQueryWrapper = new QueryWrapper<>();
        userWalletQueryWrapper.eq("user_id",userId);
        UserWallet userWallet = getOne(userWalletQueryWrapper);
        if(userWallet==null){
            //存之前也可以验证一下这个userId是否存在

            //为空要把当前的这个人存入钱包表中
            UserWallet userWallet1 = new UserWallet();
            userWallet1.setBalance(BigDecimal.valueOf(0L));
            userWallet1.setUserId(userId);
            save(userWallet1);
            return BigDecimal.valueOf(0L);
        }
        return userWallet.getBalance();
    }

    /**
     * 用户消费100元的接口
     * @param userId
     * @param money
     * @return
     */
    @Override

    @Transactional(rollbackFor = Exception.class)
    public Boolean consume(String userId, String money) {

        //钱包表 消费记录表
        QueryWrapper<UserWallet> userWalletQueryWrapper = new QueryWrapper<>();
        userWalletQueryWrapper.eq("user_id",userId);
        UserWallet userWallet = getOne(userWalletQueryWrapper);

        BigDecimal userBalance = userWallet.getBalance();

        BigDecimal moneyBig = new BigDecimal(money);

        //钱不够
        if(userBalance.compareTo(moneyBig)<0){
            return false;
        }else{
            //钱够了
            userWallet.setBalance(userBalance.subtract(moneyBig));
            this.updateById(userWallet);

            //详细表
            UserWalletRecord userRecord = new UserWalletRecord();
            userRecord.setUserId(userId);
            userRecord.setCreateTime(new Date());
            userRecord.setUpdateTime(new Date());
            userRecord.setTradeName("消费" + money + "元");
            userRecord.setTradeType(1);
            userWalletRecordService.save(userRecord);

            return true;
        }

    }

    /**
     * 用户退款20元接口
     * @param userId
     * @param money
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean refund(String userId, String money) {
        //钱包表 消费记录表
        QueryWrapper<UserWallet> userWalletQueryWrapper = new QueryWrapper<>();
        userWalletQueryWrapper.eq("user_id",userId);
        UserWallet userWallet = getOne(userWalletQueryWrapper);

        BigDecimal userBalance = userWallet.getBalance();

        BigDecimal moneyBig = new BigDecimal(money);


        //钱够了
        userWallet.setBalance(userBalance.add(moneyBig));
        this.updateById(userWallet);

        //详细表
        UserWalletRecord userRecord = new UserWalletRecord();
        userRecord.setUserId(userId);
        userRecord.setCreateTime(new Date());
        userRecord.setUpdateTime(new Date());
        userRecord.setTradeName("退款" + money + "元");
        userRecord.setTradeType(2);
        userWalletRecordService.save(userRecord);

        return true;

    }
}
